home *** CD-ROM | disk | FTP | other *** search
- unit uSelectContact;
-
- {
- *******************************************************************************
- * Descriptions: Selecting Multiple Contacts
- * $Source: /cvsroot/fma/fma/uSelectContact.pas,v $
- * $Locker: $
- *
- * Todo:
- *
- * Change Log:
- * $Log: uSelectContact.pas,v $
- * Revision 1.10.6.1 2005/01/25 16:03:20 z_stoichev
- * Merged with 2.1 Beta 1 bugfixes
- *
- * Revision 1.10 2004/06/29 11:51:15 z_stoichev
- * Added Select One Contact support
- *
- * Revision 1.9 2004/06/19 11:16:06 z_stoichev
- * - Fixed Select contacts phone lookup.
- * - Changed Contact Group icons.
- *
- * Revision 1.8 2004/05/19 18:34:16 z_stoichev
- * Build 0.1.0.35c
- *
- * Revision 1.7 2004/04/01 15:05:49 z_stoichev
- * GUI changes
- * support for unknown contacts
- *
- * Revision 1.6 2003/12/16 17:38:38 z_stoichev
- * Add support for contacts without numbers.
- *
- * Revision 1.5 2003/12/01 12:21:28 z_stoichev
- * Add filter for mobile numbers only.
- *
- * Revision 1.4 2003/11/28 09:38:07 z_stoichev
- * Merged with branch-release-1-1 (Fma 0.10.28c)
- *
- * Revision 1.3.2.4 2003/11/10 14:03:10 z_stoichev
- * RC3
- *
- * Revision 1.3.2.3 2003/10/30 15:17:18 z_stoichev
- * Remove dublicated contacts issued from Groups.
- *
- * Revision 1.3.2.2 2003/10/29 16:22:42 z_stoichev
- * Add Groups support.
- *
- * Revision 1.3.2.1 2003/10/27 07:22:54 z_stoichev
- * Build 0.1.0 RC1 Initial Checkin.
- *
- * Revision 1.3 2003/10/21 08:51:52 z_stoichev
- * Added SelContacts property containing the
- * selected recipients addresses in format
- * "name [number]; ..."
- *
- * Revision 1.2 2003/10/15 16:14:17 z_stoichev
- * Select contacts by row, not by name.
- * Corrected some typos.
- * Added Clear button to delete all recipients.
- * Added header comments.
- *
- *
- *
- *******************************************************************************
- }
-
- interface
-
- uses
- Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
- Dialogs, ExtCtrls, StdCtrls, ComCtrls, TntComCtrls, TntStdCtrls, ImgList,
- Menus, Placemnt;
-
- type
- TfrmSelContact = class(TForm)
- Label1: TLabel;
- TntListView1: TTntListView;
- AddButton: TButton;
- SelectedList: TTntListView;
- Label2: TLabel;
- RemButton: TButton;
- OkButton: TButton;
- CancelButton: TButton;
- TntEdit1: TTntEdit;
- Label3: TLabel;
- ComboBox1: TComboBox;
- ClearButton: TButton;
- ImageList1: TImageList;
- CheckBox1: TCheckBox;
- PopupMenu1: TPopupMenu;
- AddContact1: TMenuItem;
- N1: TMenuItem;
- Properties1: TMenuItem;
- FormStorage1: TFormStorage;
- procedure TntListView1SelectItem(Sender: TObject; Item: TListItem;
- Selected: Boolean);
- procedure SelectedListSelectItem(Sender: TObject; Item: TListItem;
- Selected: Boolean);
- procedure RemButtonClick(Sender: TObject);
- procedure AddButtonClick(Sender: TObject);
- procedure TntEdit1Change(Sender: TObject);
- procedure TntListView1DblClick(Sender: TObject);
- procedure ClearButtonClick(Sender: TObject);
- procedure TntListView1Insert(Sender: TObject; Item: TListItem);
- procedure Properties1Click(Sender: TObject);
- procedure AddContact1Click(Sender: TObject);
- procedure FormShow(Sender: TObject);
- private
- { Private declarations }
- procedure AddContact(Node: TTntListItem);
- procedure UpdateContacts(Mask: string = '');
- procedure InitContacts;
- function get_Sel: string;
- procedure set_Sel(const Value: string);
- function Get_MultiSelect: boolean;
- procedure Set_MultiSelect(const Value: boolean);
- public
- { Public declarations }
- property AllowMultiSelect: boolean read Get_MultiSelect write Set_MultiSelect;
- property SelContacts: string read get_Sel write set_Sel;
- end;
-
- var
- frmSelContact: TfrmSelContact;
-
- implementation
-
- uses
- uSyncPhonebook, uMissedCalls, Unit1, uSIMEdit;
-
- {$R *.dfm}
-
- procedure TfrmSelContact.TntListView1SelectItem(Sender: TObject;
- Item: TListItem; Selected: Boolean);
- begin
- AddButton.Enabled := (TntListView1.SelCount <> 0) and
- (AllowMultiSelect or (SelectedList.Items.Count = 0));
- Properties1.Enabled := (TntListView1.SelCount = 1) and
- (TntListView1.Selected.ImageIndex = 0); // only for contacts
- end;
-
- procedure TfrmSelContact.SelectedListSelectItem(Sender: TObject;
- Item: TListItem; Selected: Boolean);
- begin
- RemButton.Enabled := Selected;
- end;
-
- procedure TfrmSelContact.RemButtonClick(Sender: TObject);
- var
- i: integer;
- begin
- for i := SelectedList.Items.Count-1 downto 0 do
- if SelectedList.Items[i].Selected then
- SelectedList.Items[i].Delete;
- end;
-
- procedure TfrmSelContact.AddButtonClick(Sender: TObject);
- var
- i: integer;
- begin
- for i := 0 to TntListView1.Items.Count-1 do
- if TntListView1.Items[i].Selected then
- AddContact(TntListView1.Items[i]);
- end;
-
- procedure TfrmSelContact.TntEdit1Change(Sender: TObject);
- begin
- CheckBox1.Enabled := ComboBox1.ItemIndex = 0;
- UpdateContacts(TntEdit1.Text);
- end;
-
- procedure TfrmSelContact.UpdateContacts(Mask: string);
- var
- root: TtntTreeNode;
- i,j: integer;
- nme,tel: WideString;
- begin
- case ComboBox1.ItemIndex of
- 0: root := Form1.FNodeContactsME;
- 1: root := Form1.FNodeContactsSM;
- else exit; // just in case
- end;
- Mask := WideUpperCase(Mask);
- TntListView1.Items.BeginUpdate;
- try
- TntListView1.Items.Clear;
- if AllowMultiSelect and (ComboBox1.ItemIndex = 0) then
- for i := 0 to Form1.FNodeGroups.Count-1 do begin
- nme := Form1.FNodeGroups.Item[i].Text;
- if (Mask = '') or (Pos(Mask,WideUpperCase(nme)) <> 0) then begin
- tel := '';
- for j := 0 to Form1.FNodeGroups.Item[i].Count-1 do begin
- if length(tel) <> 0 then tel := tel + ',';
- tel := tel + Form1.FNodeGroups.Item[i].Item[j].Text;
- end;
- with TntListView1.Items.Add do begin
- Caption := nme;
- SubItems.Add(tel);
- ImageIndex := 1;
- //StateIndex := -1;
- end;
- end;
- end;
- for i := 0 to root.Count-1 do begin
- nme := root.Item[i].Text;
- if (Mask = '') or (Pos(Mask,WideUpperCase(nme)) <> 0) then
- for j := 0 to root.Item[i].Count-1 do
- if not CheckBox1.Enabled or not CheckBox1.Checked or
- (root.Item[i].Item[j].ImageIndex = 10) then
- begin
- tel := root.Item[i].Item[j].Text;
- with TntListView1.Items.Add do begin
- Caption := nme;
- SubItems.Add(tel);
- ImageIndex := 0;
- StateIndex := root.Item[i].Item[j].ImageIndex;
- end;
- end;
- end;
- finally
- TntListView1.Items.EndUpdate;
- end;
- end;
-
- procedure TfrmSelContact.TntListView1DblClick(Sender: TObject);
- begin
- if TntListView1.Selected <> nil then AddButton.Click;
- end;
-
- procedure TfrmSelContact.ClearButtonClick(Sender: TObject);
- begin
- SelectedList.Clear;
- end;
-
- function TfrmSelContact.get_Sel: string;
- var
- i: integer;
- begin
- Result := '';
- for i := 0 to SelectedList.Items.Count-1 do begin
- if Result <> '' then Result := Result + '; ';
- if SelectedList.Items[i].ImageIndex = 0 then
- if SelectedList.Items[i].Caption <> '' then
- Result := Result + SelectedList.Items[i].Caption + ' [' + SelectedList.Items[i].SubItems[0] + ']'
- else
- Result := Result + SelectedList.Items[i].SubItems[0]
- else
- Result := Result + SelectedList.Items[i].Caption + ' {' + SelectedList.Items[i].SubItems[0] + '}'
- end;
- end;
-
- procedure TfrmSelContact.set_Sel(const Value: string);
- var
- i,ptype: integer;
- d,s,nme,num: string;
- IsGroup: boolean;
- procedure LookupPerson(phone: string; var cname: string; var ptype: integer);
- var
- i: integer;
- begin
- for i := 0 to TntListView1.Items.Count-1 do
- // TODO: Add partial number matching
- if WideCompareText(TntListView1.Items[i].SubItems[0],phone) = 0 then begin
- cname := TntListView1.Items[i].Caption;
- ptype := TntListView1.Items[i].StateIndex;
- break;
- end;
- end;
- begin
- InitContacts;
- SelectedList.Clear;
- d := Trim(Value);
- repeat
- nme := '';
- ptype := 13;
- i := Pos(';',d);
- if i = 0 then i := Length(d)+1;
- s := Trim(Copy(d,1,i-1));
- Delete(d,1,i);
- IsGroup := False;
- if s <> '' then begin
- { is it a group? }
- if s[length(s)] = '}' then begin
- IsGroup := True;
- Delete(s,length(s),1);
- i := Pos('{',s);
- if i <> 0 then begin
- nme := Trim(Copy(s,1,i-1));
- Delete(s,1,i);
- end;
- end
- else begin
- if s[length(s)] = ']' then Delete(s,length(s),1);
- i := Pos('[',s);
- { ignore name, we'll lookup for it from phonebook }
- if i <> 0 then Delete(s,1,i);
- if s <> '' then begin
- num := Form1.LookupNumber(s); // if we have name instead of number,
- if num <> '' then s := num; // swap them...
- LookupPerson(s,nme,ptype);
- end;
- end;
- end;
- if s <> '' then
- with SelectedList.Items.Add do begin
- if nme = '' then nme := Form1.LookupContact(s,sUnknownContact);
- Caption := nme;
- SubItems.Add(s);
- if IsGroup then
- ImageIndex := 1
- else
- StateIndex := ptype;
- if not AllowMultiSelect then break;
- end;
- until d = '';
- end;
-
- procedure TfrmSelContact.TntListView1Insert(Sender: TObject;
- Item: TListItem);
- begin
- Item.ImageIndex := 0;
- end;
-
- procedure TfrmSelContact.Properties1Click(Sender: TObject);
- var
- ContactME: PContactData;
- ContactSM: PSIMData;
- b: boolean;
- begin
- b := False;
- case ComboBox1.ItemIndex of
- 0: begin // Phonebook
- if Form1.frmSyncPhonebook.FindContact(TntListView1.Selected.Caption,ContactME) then
- with Form1.frmSyncPhonebook do begin
- SelContact := ContactME;
- b := DoEdit;
- end;
- end;
- 1: begin // SIM
- if Form1.frmSIMEdit.FindContact(TntListView1.Selected.Caption,ContactSM) then
- with Form1.frmSIMEdit do begin
- SelContact := ContactSM;
- b := DoEdit;
- end;
- end;
- end;
- if b then UpdateContacts(TntEdit1.Text);
- end;
-
- procedure TfrmSelContact.AddContact1Click(Sender: TObject);
- var
- b: boolean;
- begin
- b := False;
- case ComboBox1.ItemIndex of
- 0: b := Form1.frmSyncPhonebook.DoEdit(True); // Phonebook
- 1: b := Form1.frmSIMEdit.DoEdit(True); // SIM
- end;
- if b then UpdateContacts(TntEdit1.Text);
- end;
-
- procedure TfrmSelContact.AddContact(Node: TTntListItem);
- var
- i: integer;
- function RemoveDub: boolean;
- var
- i,j,k: integer;
- sl: TStringList;
- Data: PContactData;
- s: string;
- begin
- Result := False;
- try
- sl := TStringList.Create;
- try
- for i := 0 to SelectedList.Items.Count-1 do
- { Find groups }
- if SelectedList.Items[i].ImageIndex = 1 then begin
- { Parse all members }
- sl.CommaText := SelectedList.Items[i].SubItems[0];
- { Replace names with default numbers }
- for j := 0 to sl.Count-1 do begin
- Form1.frmSyncPhonebook.FindContact(sl[j],Data);
- s := GetContactDefPhone(Data);
- sl[j] := s;
- end;
- { Find numbers and remove dubs }
- for j := 0 to sl.Count-1 do
- for k := 0 to SelectedList.Items.Count-1 do
- if sl[j] = SelectedList.Items[k].SubItems[0] then begin
- { Dublicate, so delete it }
- SelectedList.Items[k].Delete;
- Result := True;
- Abort; // list is modified, we have to cancel loops now
- end;
- end;
- finally
- sl.Free;
- end;
- except
- end;
- end;
- begin
- { first check if contact is already added }
- for i := 0 to SelectedList.Items.Count-1 do
- if SelectedList.Items[i].ImageIndex = Node.ImageIndex then begin
- if (SelectedList.Items[i].Caption = Node.Caption) and
- (SelectedList.Items[i].SubItems[0] = Node.SubItems[0]) then
- exit;
- end;
- { ok, add it }
- with SelectedList.Items.Add do begin
- Caption := Node.Caption;
- SubItems.Add(Node.SubItems[0]);
- ImageIndex := Node.ImageIndex;
- StateIndex := Node.StateIndex;
- end;
- { Remove dublicates (Groups + Contact) }
- while RemoveDub do ;
- end;
-
- procedure TfrmSelContact.FormShow(Sender: TObject);
- begin
- InitContacts;
- end;
-
- procedure TfrmSelContact.InitContacts;
- begin
- if TntListView1.Items.Count = 0 then
- FormStorage1.RestoreFormPlacement;
- if TntListView1.Items.Count = 0 then
- TntEdit1Change(nil);
- end;
-
- function TfrmSelContact.Get_MultiSelect: boolean;
- begin
- Result := TntListView1.MultiSelect;
- end;
-
- procedure TfrmSelContact.Set_MultiSelect(const Value: boolean);
- begin
- TntListView1.MultiSelect := Value;
- if not Value then
- while SelectedList.Items.Count > 1 do
- SelectedList.Items.Delete(1);
- end;
-
- end.
-